/**
 * PopLayer 弹出层
 * 
 * @return {[type]}
 */
include("mod/mask");

(function($){
    $.centerSize = function(size){
        var view = {
                width   : $(window).width(),
                height  : $(window).height()
            },
            scrollTop = $(window).scrollTop();
        return {
            top: Math.max(scrollTop + (view.height - size.height)/2, 0),
            left:Math.max((view.width - size.width)/2, 0)
        };
    };
    $.center = function(el){
        el = $(el);
        el.css('position','absolute')
            .css($.centerSize({
                width   : el.outerWidth(),
                height  : el.outerHeight()
            }));
    };
})(jQuery);

/**
 * [PopLayer description]
 * @param {[type]} trigger [description]
 * @param {[type]} target  [description]
 * @param {[type]} options [description]
 */
var PopLayer = function(options){
	options = options || {};
	var me = this,
        defaultFn = function(){return true;};
	me.options = $.extend({
		beforeOpen : defaultFn,
        beforeClose: defaultFn,
		afterClose: defaultFn,
        ok: defaultFn,
        cancel: defaultFn
        ,iframe: false
	},options);
    options.trigger && (me.trigger = $(options.trigger));
    options.target && (me.target = $(options.target));

	me.init();
	//me.trigger = $(trigger);
	//me.target = $(target);
};

$.extend(PopLayer.prototype, {
	init: function(){
		this._initEvent();
	},
	_initEvent: function(){
		var me = this, o = me.options, root, isIframe;
		me.trigger && me.trigger.click(function(evt){
            me.open();
            evt.preventDefault();
            //evt.stopPropagation();    
        });
        if(me.target){
            isIframe = o.iframe && me.target[0].tagName.toLowerCase() == 'iframe';
            //去掉Iframe模式的支持, 因为IE6下会报告权限错误.
            if(isIframe){
                return;
            }
            function bindAction(){
                //root = isIframe ? $(me.target[0].contentWindow.document) : me.target;
                root = me.target;
                root.find('.close, .cancel').click(function(evt){
                    o.cancel();
                    me.close();
                    evt.preventDefault();
                });
                root.find('.ok').click(function(evt){
                    o.ok();
                    me.close(); 
                    evt.preventDefault();
                });
            }
            //isIframe ? me.target.load(bindAction) : bindAction();
            bindAction();
        }
	},
	open: function(){
        var me = this, o = me.options;
        if(o.beforeOpen()){
            me.update();
        }
	},
	close: function(){
        var me = this, o = me.options;
        if(o.beforeClose()){
            mask.off();
            me.target.hide();
			var target = me.target.get(0);
			if(target && target.tagName && target.tagName.toUpperCase()=='IFRAME'){
				me.target.remove();
			}
            o.afterClose();
			try
			{
				me.loading.remove();
			}
			catch(e)
			{}
            PopLayer.current = null;
        }
	},
	update: function(){
        PopLayer.current = this;
        mask.on();
        this.target.show();
        $.center(this.target);
	}
});

PopLayer.iframe = function(url){
    var rtn, 
        loading = $('<img width="48" src="/static/img/loading.gif?v=1">').css("zIndex",mask.zIndex+1),
        frame = $('<iframe class="dialog" frameborder="0" scrolling="no"></iframe>')
        .css({position:'absolute',left:-9999, top:-9999, zIndex:9999, width:1, height:1})
        .appendTo(document.body)
        .attr('src', url);
    rtn = new PopLayer({target:frame, iframe:true});
    /*
    frame.load(function(){
        var size, 
            win = frame[0].contentWindow,
            oriSize = {
                width : frame.width(),
                height: frame.height()
            };
        frame.css({width:1, height:1});
        //去掉0和1的影响
        function fixSize(a, b){
            return a > 1 ? a : b;
        }
        size = {
            //body.scrollWidth 用来解决ie下document与浏览器大小相同的问题
            width : fixSize( $(win.document.body)[0].scrollWidth, $(win.document).outerWidth()),
            height: fixSize( $(win.document.body)[0].scrollHeight, $(win.document).outerHeight())
        };
        frame.css(oriSize);
        frame.animate($.extend(size, $.centerSize(size)), {duration:200});
        loading.remove();
    });
    */
    rtn.open();
    $.center(loading.appendTo(document.body));
    PopLayer.current.loading = loading;
    return rtn;
};
PopLayer.close = function(){
    PopLayer.current && PopLayer.current.close();
};
/**
 * @param size: {width:VAL, height:VAL}
 */
PopLayer.resize = function(size){
    if(PopLayer.current){
        PopLayer.current.target.animate($.extend(size, $.centerSize(size)), {duration:200});
        PopLayer.current.loading.remove();
    }
}
